package su.myspringapps.onlineschool.repository;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import su.myspringapps.onlineschool.entity.Student;

import java.util.Date;
import java.util.List;

@SpringBootTest
class HQLStudentRepositoryTest {

    @Autowired
    HQLStudentRepository hqlStudentRepository;

    @Test
    void testCreate() throws Exception {
        Student expectedStudent = new Student();
        expectedStudent.setName("Навотно Стоечко");
        expectedStudent.setAge(18);
        expectedStudent.setRegistrationDate(new Date());
        Integer id = hqlStudentRepository.create(expectedStudent);
        Student actualStudent = hqlStudentRepository.findById(id);
        Assertions.assertEquals(expectedStudent, actualStudent);
        hqlStudentRepository.delete(expectedStudent);
        actualStudent = hqlStudentRepository.findById(id);
        Assertions.assertNull(actualStudent);
    }

    @Test
    void testUpdate() throws Exception {
        Student newStudent = new Student();
        newStudent.setAge(20);
        newStudent.setName("Таня Чумовоз");
        newStudent.setRegistrationDate(new Date());
        Integer id = hqlStudentRepository.create(newStudent);
        Student studentForUpdate = hqlStudentRepository.findById(id);
        Assertions.assertEquals(newStudent, studentForUpdate);
        String newName = "Валя Полотер";
        studentForUpdate.setName(newName);
        hqlStudentRepository.update(studentForUpdate);
        Student actualStudent = hqlStudentRepository.findById(id);
        Assertions.assertNotEquals(newStudent, actualStudent);
        Assertions.assertEquals(newName, actualStudent.getName());
        hqlStudentRepository.delete(actualStudent);
        Assertions.assertNull(hqlStudentRepository.findById(id));
    }

    @Test
    void updateParameter() throws Exception {
        Student newStudent = new Student();
        newStudent.setAge(23);
        newStudent.setName("Семарь Здрахарь");
        newStudent.setRegistrationDate(new Date());
        Integer id = hqlStudentRepository.create(newStudent);
        Student studentForUpdate = hqlStudentRepository.findById(id);
        Assertions.assertEquals(newStudent, studentForUpdate);
        String newName = "Шантор Червиз";
        Integer newAge = 22;
        hqlStudentRepository.updateParameter(id, "name", newName);
        hqlStudentRepository.updateParameter(id, "age", newAge);
        Student actualStudent = hqlStudentRepository.findById(id);
        Assertions.assertEquals(newName, actualStudent.getName());
        Assertions.assertEquals(newAge, actualStudent.getAge());
        hqlStudentRepository.deleteById(id);
        actualStudent = hqlStudentRepository.findById(id);
        Assertions.assertNull(actualStudent);
    }

    @Test
    void testDelete() throws Exception {
        Student studentForDelete = new Student();
        studentForDelete.setAge(19);
        studentForDelete.setName("Седайко Стюмчик");
        studentForDelete.setRegistrationDate(new Date());
        Integer id = hqlStudentRepository.create(studentForDelete);
        Student actualStudent = hqlStudentRepository.findById(id);
        Assertions.assertEquals(studentForDelete, actualStudent);
        hqlStudentRepository.delete(studentForDelete);
        actualStudent = hqlStudentRepository.findById(id);
        Assertions.assertNull(actualStudent);
    }

    @Test
    void deleteById() throws Exception {
        Student studentForDelete = new Student();
        studentForDelete.setAge(19);
        studentForDelete.setName("Седайко Стюмчик");
        studentForDelete.setRegistrationDate(new Date());
        Integer id = hqlStudentRepository.create(studentForDelete);
        Student actualStudent = hqlStudentRepository.findById(id);
        Assertions.assertEquals(studentForDelete, actualStudent);
        hqlStudentRepository.deleteById(id);
        actualStudent = hqlStudentRepository.findById(id);
        Assertions.assertNull(actualStudent);
    }

    @Test
    void testFindById() throws Exception {
        Student actualStudent = hqlStudentRepository.findById(1);
        Assertions.assertEquals(1, actualStudent.getId());
        Assertions.assertEquals("Фуриков Эрнст", actualStudent.getName());
        Assertions.assertEquals(18, actualStudent.getAge());
        Assertions.assertEquals("2016-01-01 00:00:00.0", actualStudent.getRegistrationDate().toString());
    }

    @Test
    void testFindAll() throws Exception {
        List<Student> studentList = hqlStudentRepository.findAll();
        Student actualStudent1 = studentList.get(0);
        Student actualStudent2 = studentList.get(1);
        Student actualStudent3 = studentList.get(2);
        Student actualStudent4 = studentList.get(3);

        Assertions.assertEquals(2, actualStudent1.getId());
        Assertions.assertEquals("Амбражевич Порфирий", actualStudent1.getName());
        Assertions.assertEquals(19, actualStudent1.getAge());

        Assertions.assertEquals(3, actualStudent2.getId());
        Assertions.assertEquals("Носов Макар", actualStudent2.getName());
        Assertions.assertEquals(20, actualStudent2.getAge());

        Assertions.assertEquals(4, actualStudent3.getId());
        Assertions.assertEquals("Кооскора Вениамин", actualStudent3.getName());
        Assertions.assertEquals(21, actualStudent3.getAge());

        Assertions.assertEquals(5, actualStudent4.getId());
        Assertions.assertEquals("Беленков Денис", actualStudent4.getName());
        Assertions.assertEquals(22, actualStudent4.getAge());
    }
}